package com.zhuchaooasystem.ui.home.order;

import android.content.Intent;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.zhuchaooasystem.R;
import com.zhuchaooasystem.adapter.holder.OrderDetailHolder;
import com.zhuchaooasystem.base.BaseActivity;
import com.zhuchaooasystem.common.Key;
import com.zhuchaooasystem.entity.OrderDetailEntity;
import com.zhuchaooasystem.presenter.home.order.OrderDetailContract;
import com.zhuchaooasystem.presenter.home.order.OrderDetailPresenter;
import com.zhuchaooasystem.utils.DoubleUtils;
import com.zhuchaooasystem.widget.StateButton;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class OrderDetailActivity extends BaseActivity<OrderDetailPresenter>
        implements OrderDetailContract.View {

    @BindView(R.id.actionbar_iv_back)
    ImageView actionbarIvBack;
    @BindView(R.id.actionbar_txt_title)
    TextView actionbarTxtTitle;
    @BindView(R.id.act_order_detail_btn_tracking)
    StateButton btn_tracking;

    @BindView(R.id.order_detial_state_image)
    ImageView image_state;
    @BindView(R.id.order_detial_id)
    TextView tv_orderId;
    @BindView(R.id.order_detial_price)
    TextView tv_price;
    @BindView(R.id.order_detial_date)
    TextView tv_data;

    @BindView(R.id.order_detial_name)
    TextView tv_name;
    @BindView(R.id.order_detial_phone)
    TextView tv_phone;
    @BindView(R.id.order_detial_address)
    TextView tv_address;
    @BindView(R.id.act_order_detail_recyclerView)
    EasyRecyclerView recyclerView;
    @BindView(R.id.act_order_detail_more_good)
    StateButton btn_more_good;
    @BindView(R.id.order_detial_remark)
    TextView tv_remark;
    @BindView(R.id.order_detial_total_price)
    TextView tv_total_price;
    @BindView(R.id.order_detial_balance)
    TextView tv_balance;
    @BindView(R.id.order_detial_freight)
    TextView tv_freight;
    @BindView(R.id.order_detial_install)
    TextView tv_install;
    @BindView(R.id.order_detial_discount)
    TextView tv_discount;
    @BindView(R.id.order_detial_cash)
    TextView tv_cash;
    @BindView(R.id.order_detial_blance_pay)
    TextView tv_balance_pay;
    @BindView(R.id.order_detial_over_pay)
    TextView tv_over_pay;
    @BindView(R.id.order_detial_shold_pay)
    TextView tv_should_pay;
    @BindView(R.id.act_order_detail_good_num)
    TextView tv_good_num;
    @BindView(R.id.order_detial_sv)
    NestedScrollView scrollView;

    @BindView(R.id.order_detial_adv)
    LinearLayout layout_adv;


    private String orderid = "";
    private RecyclerArrayAdapter adapter;
    private List<OrderDetailEntity.OrderBean.DetailsBean> list_good;
    private OrderDetailEntity bean;


    @Override
    protected int getLayout() {
        return R.layout.activity_order_detail;
    }

    @Override
    protected OrderDetailPresenter getPresenter() {
        return new OrderDetailPresenter(mContext, this);
    }

    @Override
    protected void initVariables() {
        Intent intent = getIntent();
        orderid = intent.getStringExtra(Key.ORIDERID);
    }

    @Override
    protected void initView() {

        list_good = new ArrayList<>();

        actionbarIvBack.setVisibility(View.VISIBLE);
        actionbarTxtTitle.setText("订单详情");

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));

        recyclerView.setAdapter(adapter = new RecyclerArrayAdapter(mContext) {
                    @Override
                    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {

                        return new OrderDetailHolder(parent);
                    }
                }
        );

        mPresenter.downOrderDetail(orderid);
//        mPresenter.downOrderDetail("12726");
    }

    @Override
    protected void initDataAndLoadData() {

    }

    @Override
    public void showOederDetail(OrderDetailEntity entity) {

        scrollView.setVisibility(View.VISIBLE);

        bean = entity;

        //判断显示更多商品是不是显示
        if (entity.getOrder().getDetails().size() > 2) {
            btn_more_good.setVisibility(View.VISIBLE);
        } else {
            btn_more_good.setVisibility(View.GONE);
        }

        //订单跟踪
        if (entity.getOrder().getStatus() == 0 || entity.getOrder().getStatus() == -1) {
            btn_tracking.setVisibility(View.GONE);
        } else {
            btn_tracking.setVisibility(View.VISIBLE);
        }

        if (entity.getOrder().getOrderState().equals("等待发货")) {
            image_state.setImageResource(R.drawable.ic_wait_send_big);
            layout_adv.setVisibility(View.VISIBLE);
        } else if (entity.getOrder().getOrderState().equals("部分发货")) {
            image_state.setImageResource(R.drawable.ic_little_send_big);
            layout_adv.setVisibility(View.VISIBLE);
        } else if (entity.getOrder().getOrderState().equals("已发货")) {
            image_state.setImageResource(R.drawable.ic_send_big);
            layout_adv.setVisibility(View.GONE);
        } else if (entity.getOrder().getOrderState().equals("订单取消")) {
            image_state.setImageResource(R.drawable.ic_order_cancel_big);
            layout_adv.setVisibility(View.GONE);
        }

        tv_orderId.setText("订单编号：" + entity.getOrder().getOrdercode() + "");
        tv_price.setText("总金额：" + "￥" + DoubleUtils.getDouble(entity.getOrder().getTotal()));
        tv_data.setText("订单日期：" + entity.getOrder().getTime());
        tv_name.setText("收货人：" + entity.getOrder().getContactName());
        tv_phone.setText("电话：86-" + entity.getOrder().getPhone());
        tv_address.setText("收货地址：" + entity.getOrder().getAddress());
        if (entity.getOrder().getRemark() == null || entity.getOrder().getRemark().equals("")) {

            tv_remark.setText("暂无备注");

        } else {
            tv_remark.setText(entity.getOrder().getRemark());

        }

        tv_total_price.setText("￥" + DoubleUtils.getDouble(entity.getOrder().getTotal()));
        tv_balance.setText("+￥" + DoubleUtils.getDouble(entity.getOrder().getDiffPrice()));
        tv_install.setText("+￥" + DoubleUtils.getDouble(entity.getOrder().getInstallationfee()));
        tv_freight.setText("+￥" + DoubleUtils.getDouble(entity.getOrder().getFreight()));
        tv_discount.setText("-￥" + DoubleUtils.getDouble(entity.getOrder().getDiscount()));
        tv_cash.setText("-￥" + DoubleUtils.getDouble(entity.getOrder().getTicket()));
        tv_balance_pay.setText("-￥" + DoubleUtils.getDouble(entity.getOrder().getRecharge()));
        tv_over_pay.setText("-￥" + DoubleUtils.getDouble(entity.getOrder().getCollect()));
        tv_should_pay.setText("￥" + DoubleUtils.getDouble(entity.getOrder().getDeelWith()));

        //共几件商品
        tv_good_num.setText(entity.getOrder().getDetails().size() + "");

        //给商品详情添加数据---默认展示两条数据
        if (entity.getOrder().getDetails().size() < 3) {
            adapter.clear();
            adapter.addAll(entity.getOrder().getDetails());
        } else {
            for (int i = 0; i < 2; i++) {
                list_good.add(entity.getOrder().getDetails().get(i));
            }
            adapter.clear();
            adapter.addAll(list_good);
        }

        scrollView.setVisibility(View.VISIBLE);
    }


    private void loadMoreGood() {
        adapter.clear();
        adapter.addAll(bean.getOrder().getDetails());
    }

    private void showTwoGood() {
        adapter.clear();
        list_good = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            list_good.add(bean.getOrder().getDetails().get(i));
        }
        adapter.addAll(list_good);
    }

    @OnClick({R.id.actionbar_iv_back, R.id.act_order_detail_btn_tracking, R.id.act_order_detail_more_good})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.actionbar_iv_back:

                OrderDetailActivity.this.finish();

                break;
            //订单跟踪
            case R.id.act_order_detail_btn_tracking:
                Intent intent = new Intent(OrderDetailActivity.this, OrderTrackActivity.class);
                intent.putExtra(Key.ORIDERID, bean.getOrder().getOrderid() + "");
                startActivity(intent);
                break;
            //查看更多商品
            case R.id.act_order_detail_more_good:

                if (btn_more_good.getText().toString().equals("查看更多商品")) {
                    btn_more_good.setText("收起更多商品");
                    loadMoreGood();
                } else if (btn_more_good.getText().toString().equals("收起更多商品")) {
                    btn_more_good.setText("查看更多商品");
                    showTwoGood();
                }

                break;
        }
    }
}
